//绘制横向柱形图
function DRAWCROSSCHART(url,modelSend,drawContainer) {
    this.draw = function () {
        console.log(modelSend);
        $.ajax({ type: "post",
            url: url,
            dataType: "json",
            data:{"paramData":JSON.stringify(modelSend)},
            success: function (result) {
                console.log(result);
                var dcInstance= new DRAWCROSS(result,drawContainer);
                dcInstance.draw(result,drawContainer);
            },
            error: function (err) {
                console.log("服务器链接失败");
            }});
    };
};
//定义一个用于绘制图形的类
function DRAWCROSS(json,drawContainer){
    this.colorg=['#357FB7','#D95550','#9AC062'];
    //根据得到的数据绘制Y轴
    this.setYaxis=function(data){
            var yaxArray = [];
            for (var i = 0; i < data.axis.yCnt; i++) {
                var k = i;
                var obj = {labels:{
                        formatter:function(){
                            return Highcharts.numberFormat(this.value,0,".",",");
                        },
                        style:{color:''}},
                    title:{text:''}};
                obj.labels.style.color =data.data[k]? this.colorg[data.data[k].yAxis]:"none";
                if(data.theme.yAxis[k].align=='right'||data.theme.yAxis[k].opposite=='false'){
                    obj.labels.formatter=function(){
                        return Highcharts.numberFormat(this.value,0,".",",")+"%";
                    };
                    obj.opposite='true';
                }
                yaxArray.push(obj);
            }
            return yaxArray;
    };
    //根据得到的数据设置指标
    this.setSeries=function(dataSource){
        var serArray=[];
        for(var key in dataSource.data){
            var obj={};
            var num=dataSource.data[key].yAxis;
            // console.log(num);
            obj.name=dataSource.data[key].lable;
            obj.type=dataSource.theme.yAxis[num].type;
            obj.yAxis=dataSource.data[key].yAxis;
            obj.data=dataSource.data[key].value;
            obj.color=this.colorg[key];
            obj.color=this.colorg[key];
            obj.zIndex=dataSource.theme.yAxis[num].type=='line'?1:0;
            obj.dataLabels={enabled: true,
                color: '#000000'};
            serArray.push(obj);
        }
        return serArray;
    };
    //绘制横向坐标轴的方法
    this.draw=function(json,container){
        var option={
            chart: {
                panning: true,
                type: 'bar'   //指定图表的类型，默认是折线图（line）
            },

            title: {
                text: ''     // 标题
            },
            tooltip: {
                shared: true,
                pointFormatter:function(){
                    if(this.series.name.match(/(.*?比)/)){
                    return '<span style="color: '+ this.series.color + '"></span> '+
                        this.series.name+': <b>'+ this.y +'%</b><br/>';}
                    else {
                        return '<span style="color: '+ this.series.color + '"></span> '+
                            this.series.name+': <b>'+ this.y +'</b><br/>';
                    }
                }
            },
            scrollbar : {
                enabled:true
            },
            xAxis: {
                min:0,
                max:json.axis.length,
                categories: json.axis.x   // x 轴分类
            },
            yAxis: this.setYaxis(json),
            legend:{

                align: 'center',
                verticalAlign: 'top'
            },
            exporting: {
                chartOptions: {
                    plotOptions: {
                        series: {
                            dataLabels: {
                                enabled: true
                            }
                        }
                    }
                }
            },
            series:this.setSeries(json)
        };
        Highcharts.setOptions({
            lang: {printChart:"打印图表",
                downloadJPEG: "下载JPEG 图片" ,
                downloadPDF: "下载PDF文档"  ,
                downloadPNG: "下载PNG 图片"  ,
                downloadSVG: "下载SVG 矢量图" ,
                exportButtonTitle: "导出图片"
            }
        });
        Highcharts.chart(container,option);
    };
}